package jvm;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/8/2 19:36
 */

public class TestThread extends Thread{

    private  boolean canRun=true;
    public void run(){
        System.out.println(Thread.currentThread().getId()+"  run开始");
        int i=0;
        while (canRun){
            System.out.println("run: "+Thread.currentThread().getId()+"\t"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i++;
//            if(i==10) {
//                canRun=false;
//                break;
//            }
        }
        System.out.println(Thread.currentThread().getId()+"  run退出"+"\t"+i);
    }

    public void setCanRun(boolean canRun) {
        this.canRun = canRun;
    }

    public static void main(String[] args){
        System.out.println("mainThread:\t"+Thread.currentThread().getId());
        TestThread testThread=new TestThread();
        testThread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //testThread.run();
        testThread.setCanRun(false);
        System.out.println("main exit");
    }
}
